package cease.urs;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import org.apache.http.Header;

/**
 * 存储用户登录网易通行证后返回的cookie
 * 
 * @author dhf
 */
public class Session implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 3113639381951054583L;

    private String cookieStr = "";

    private long createTime;

    private Session() {}

    private Session(String cookieStr) {
        if (null != cookieStr) {
            this.cookieStr = cookieStr;
        }
    }

    public static Session parseSetCookies(Header[] headers) {
        String cookieStr = "";
        if (null != headers && headers.length > 0) {
            for (Header h: headers) {
                if (h.getName().equalsIgnoreCase("Set-Cookie")) {
                    String cstr = h.getValue();
                    String[] parts = cstr.split(";");
                    for (String part: parts) {
                        part = part.replaceAll("\\s*", "");
                        int pos = part.indexOf("=");
                        if (-1 != pos) {
                            String key = part.substring(0, pos);
                            String value = part.substring(pos + 1);
                            if ("domain".equalsIgnoreCase(key)) {

                            } else if ("path".equalsIgnoreCase(key)) {

                            } else if ("expires".equalsIgnoreCase(key)) {

                            } else {
                                cookieStr = cookieStr + key + "=" + value + ";";
                            }
                        }
                    }
                }
            }
        }
        Session session = new Session(cookieStr);
        session.createTime = System.currentTimeMillis();
        return session;
    }

    public String getCookieStr() {
        return cookieStr;
    }

    public long getCreateTime() {
        return this.createTime;
    }

    public static void writeOut(Session session, OutputStream outputStream)
            throws IOException {
        if (null == session) {
            throw new IllegalArgumentException("[session] could not be null");
        }
        if (null == outputStream) {
            throw new IllegalArgumentException(
                    "[outputStream] could not be null");
        }
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(outputStream);
            oos.writeUTF(session.cookieStr);
            oos.writeLong(session.createTime);
        } finally {
            if (null != oos) {
                oos.close();
            }
        }
    }

    public static Session readIn(InputStream inputStream) throws IOException,
            ClassNotFoundException {
        if (null == inputStream) {
            throw new IllegalArgumentException(
                    "[inputStream] could not be null");
        }
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(inputStream);
            String cookieStr = ois.readUTF();
            long createTime = ois.readLong();
            Session session = new Session(cookieStr);
            session.createTime = createTime;
            return session;
        } finally {
            if (null != ois) {
                ois.close();
            }
        }
    }

    public static void main(String[] args) throws IOException,
            ClassNotFoundException {
        Session session = new Session("NTES_SESS=XXXXXXXX");
        session.createTime = 123456789L;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Session.writeOut(session, baos);

        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        session = Session.readIn(bais);
        System.out.println(session.getCookieStr() + ","
                + session.getCreateTime());
    }
}
